<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增广告</title>
    <style>
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #cad2e0 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        h1 {
            font-family: 'Arial', sans-serif;
            color: #333;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
            animation: fadeIn 1s ease-in-out;
        }

        .form-container {
            position: relative;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 800px;
            text-align: left;
            overflow-y: auto;
            max-height: 90vh;
        }

        .return-button {
            position: absolute;
            top: 20px;
            right: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input[type="text"],
        textarea,
        select,
        input[type="datetime-local"],
        input[type="file"] {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }

        select {
            width: 130px;
        }

        input[type="datetime-local"] {
            width: 200px;
        }

        #username {
            width: 200px;
        }

        input[type="button"] {
            width: 80px;
            padding: 8px;
            margin: 0;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
            text-align: center;
        }

        input[type="button"]:hover {
            background-color: #0056b3;
        }

        #submit-btn-container {
            display: flex;
            justify-content: center;
        }

        #submit-btn {
            width: 80px;
            padding: 8px;
            margin: 0;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
            text-align: center;
        }

        #submit-btn:hover {
            background-color: #0056b3;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
            }
            to {
                opacity: 1;
            }
        }
    </style>
</head>
<body>
<div class="form-container">
    <h1>新增广告</h1>
    <div class="return-button">
        <input type="button" value="返回" onclick="location.href='operating.html'">
    </div>
    <form>
        <div class="form-group">
            <label for="title">标题</label>
            <input type="text" id="title" name="title" placeholder="请输入广告标题">
        </div>
        <div class="form-group">
            <label for="content">内容</label>
            <textarea id="content" name="content" rows="4" placeholder="请输入广告内容"></textarea>
        </div>
        <div class="form-group">
            <label>类型</label>
            <input type="radio" id="ecommerce" name="location" onclick="updateTags()" value="网店" checked> 网店
            <input type="radio" id="advertising" name="location" onclick="updateTags()" value="新闻"> 新闻
        </div>
        <div class="form-group">
            <label for="tag">标签</label>
            <select id="tag" name="tag">
                <option value="" disabled selected>请选择标签</option>
            </select>
        </div>
        <div class="form-group">
            <label for="username">广告主</label>
            <input type="text" id="username" name="username" placeholder="请输入广告主名称" th:value="${name}">
        </div>
        <div class="form-group">
            <label for="time">发布时间</label>
            <input type="datetime-local" id="time" name="time">
        </div>
        <div class="form-group">
            <img alt="广告图片" src="" id="picture-preview" style="max-width: 100%; margin-bottom: 10px;">
            <label for="picture">图片/Logo路径</label>
            <input type="file" id="picture" name="picture"
                   placeholder="广告附图" alt="请选择本地文件"
                   onchange="catchLoaded()"
                   accept="image/*">
        </div>
        <div class="form-group">
            <div id="submit-btn-container">
                <input type="button" value="提交"
                       id="submit-btn" onclick="submitForm()">
            </div>
        </div>
    </form>
</div>

<script>
    function updateTags() {
        const tagSelect = document.getElementById('tag');
        const ecommerceCheckbox = document.getElementById('ecommerce');
        const advertisingCheckbox = document.getElementById('advertising');

        // 清空现有选项
        tagSelect.innerHTML = '<option value="" disabled selected>请选择标签</option>';

        // 根据复选框状态添加选项
        if (ecommerceCheckbox.checked) {
            tagSelect.innerHTML += '<option value="电子产品">电子产品</option>';
            tagSelect.innerHTML += '<option value="家居用品">家居用品</option>';
            tagSelect.innerHTML += '<option value="服装">服装</option>';
            tagSelect.innerHTML += '<option value="食品">食品</option>';
            tagSelect.innerHTML += '<option value="图书">图书</option>';
            tagSelect.innerHTML += '<option value="美妆">美妆</option>';
            tagSelect.innerHTML += '<option value="运动">运动</option>';
            tagSelect.innerHTML += '<option value="汽车">汽车</option>';
            tagSelect.innerHTML += '<option value="旅行">旅行</option>';
        }

        if (advertisingCheckbox.checked) {
            tagSelect.innerHTML += '<option value="电子产品">电子产品</option>';
            tagSelect.innerHTML += '<option value="美妆">美妆</option>';
            tagSelect.innerHTML += '<option value="公益">公益</option>';
            tagSelect.innerHTML += '<option value="金融">金融</option>';
            tagSelect.innerHTML += '<option value="汽车">汽车</option>';
        }
    }
</script>
<script>

    // 确保页面加载时复选框状态正确
    document.addEventListener('DOMContentLoaded', function () {
        updateTags();
    });

    function submitForm() {
        const form = document.querySelector('form');
        const formData = new FormData(form);
        const location = document.querySelector('input[name="location"]:checked').value;
        formData.append('location', location);

        fetch('api/manage/advertisements', {
            method: 'POST',
            body: formData
        })
            .then(response => response.text())
            .then(message => {
                alert(message);
                if (message.includes("成功")) {
                    window.location.href = 'operating.html';
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('提交失败，请重试！');
            });
    }

</script>
<script>
    function catchLoaded() {
        const file = document.getElementById('picture').files[0];
        console.log(file);
        let reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
            document.getElementById("picture-preview").src=reader.result;
        };
    }
</script>
</body>
</html>
